package pobidz;

import java.awt.geom.AffineTransform;
import java.awt.image.AffineTransformOp;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.jfree.chart.JFreeChart;



public class Picture {


	private String name;
	private BufferedImage bi;
	private BufferedImage orgin;
	private BufferedImage originalImage;
//	private BufferedImage usedOnlyWhenBrightnessNConstrastAreModified;
	
	private Filters filters;
	private ComplexImage complexImage;
	private int[][] fhtImage;
//	private File pictureFile;


	public Picture(String name, BufferedImage bi2)
	{
           
		this.name = name;
		this.orgin = bi2;

		this.originalImage = new BufferedImage(bi2.getWidth(), bi2.getHeight(), bi2.getType());
		this.originalImage.setData(bi2.getData());
//		this.usedOnlyWhenBrightnessNConstrastAreModified = new BufferedImage(
//				bi2.getWidth(), bi2.getHeight(), bi2.getType());
//		this.usedOnlyWhenBrightnessNConstrastAreModified.setData(bi2.getData());
              
		this.bi = bi2;
		filters = new Filters(this);
		
	}

	public Filters getFilters() {
		return filters;
	}

	public String getName() {
		return this.name;
	}

	public BufferedImage getBi() {
		return this.bi;
	}
	
	
	
	public void scalePicture(double scale){
		 AffineTransform tx = new AffineTransform();
		 tx.scale(scale,scale);
		 AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);

		 this.bi = op.filter(this.orgin, null);

		 this.originalImage = new BufferedImage(this.bi.getWidth(), this.bi.getHeight(), this.bi.getType());
		 this.originalImage.setData(this.bi.getData());
		 
	}
	
	public void setBi(BufferedImage bi)
	{
		this.bi = bi;
	}
	
	public BufferedImage getOrgin() {
		return this.orgin;
	}
	
	public void writePictureToFile(String filePath){
		
		int dotPosition = filePath.lastIndexOf(".");
		//System.out.println(filePath.substring(dotPosition));
		//TODO: zapytanie o nadpisywanie pliku
		if (dotPosition == -1)
		{
			dotPosition = filePath.length();
			StringBuilder filePathBuilder = new StringBuilder();
			filePathBuilder.append(filePath + ".jpg");
			filePath = filePathBuilder.toString();
		}
		else if (filePath.charAt(dotPosition-1) == '\\')
		{
			String noname = "Empty filename";
			
			StringBuilder filePathBuilder = new StringBuilder();
			
			filePathBuilder.append(filePath.substring(0,dotPosition)
					+ noname + filePath.substring(dotPosition, filePath.length()));
			dotPosition += noname.length();
			filePath = filePathBuilder.toString();
		}

		
		File outputFile = new File(filePath);
	    try {
			ImageIO.write(this.bi, filePath.substring(dotPosition+1) , outputFile);
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("Problem z zapisem pliku!"); //$NON-NLS-1$
		}
	}

	public BufferedImage getOriginalImage() {
		return originalImage;
	}

	public void setOriginalImage(BufferedImage originalImage) {
		this.originalImage = originalImage;
	}

	public void setOrgin(BufferedImage pic) {
		this.originalImage = new BufferedImage(pic.getWidth(), pic.getHeight(), pic.getType());
		this.originalImage.setData(pic.getData());
		this.orgin = pic;
	}

	

//	public BufferedImage getUsedOnlyWhenBrightnessNConstrastAreModified() {
//		return usedOnlyWhenBrightnessNConstrastAreModified;
//	}
//
//	public void setUsedOnlyWhenBrightnessNConstrastAreModified(
//			BufferedImage usedOnlyWhenBrightnessNConstrastAreModified) {
//		this.usedOnlyWhenBrightnessNConstrastAreModified = usedOnlyWhenBrightnessNConstrastAreModified;
//	}

	public void setComplexImage(ComplexImage complexImage)
	{
		this.complexImage = complexImage;
	}
	
	public ComplexImage getComplexImage()
	{
		return complexImage;
	}
	
	public void setFHTImage(int[][] tab)
	{
		this.fhtImage = tab;
	}
	
	public int[][] getFHTImage()
	{
		return this.fhtImage;
	}
	
}
